<?php

defined('SYSPATH') or die('No direct script access.');

class Model_Repass extends Model {

    public function generate_new_pass() {
        $new_pass = Text::random('alnum', 6);
        return $new_pass;
    }

    public function send_code($email) {
        $code = Text::random('hexdec', 32);
        $hostname = $_SERVER['HTTP_HOST'];
        $config = Kohana::$config->load('email');
        Email::connect($config);
        $to = $email;
        $subject = 'Восстановление пароля';
        $from = "hlebin@intecweb.ru";
        $message = "http://" . $hostname . "/login/repass/" . $code;
        Email::send($to, $from, $subject, $message, $html = false);
        $model_user = ORM::factory('user', array('email' => $email));
        $model_user->code = $code;
        $model_user->save();
    }

    public function save_new_pass($user_id, $pass) {
        $model_user = ORM::factory('user', $user_id);
        $model_user->password = $pass;
        $model_user->code = NULL;
        if ($model_user->loaded()) {
            $model_user->update();
            return true;
        } else {
            return false;
        }
    }

    public function send_new_pass($email, $pass) {
        $config = Kohana::$config->load('email');
        Email::connect($config);
        $to = $email;
        $subject = 'Ваш новый пароль';
        $from = "hlebin@intecweb.ru";
        $message = $pass;
        Email::send($to, $from, $subject, $message, $html = false);
    }

}